package com.bestvike.stone.core.util.area;

import com.bestvike.linq.exception.ArgumentException;
import com.bestvike.stone.core.collection.KeyedCollection;

/**
 * 省份集合
 * Created by 许崇雷 on 2018-07-28.
 */
public final class ProvinceCollection extends KeyedCollection<String, Province> {
    public ProvinceCollection() {
    }

    @Override
    protected String getKeyForItem(Province province) {
        return province.getCode();
    }

    public Province getProvince(String code) {
        if (code == null || code.length() < 2)
            throw new ArgumentException("length of 'code' must equals to or greater than 2");
        return this.get(code.substring(0, 2));
    }

    public City getCity(String code) {
        if (code == null || code.length() < 4)
            throw new ArgumentException("length of 'code' must equals to or greater than 4");
        Province province = this.getProvince(code);
        return province == null ? null : province.getCityCollection().get(code.substring(0, 4));
    }

    public County getCounty(String code) {
        if (code == null || code.length() < 6)
            throw new ArgumentException("length of 'code' must equals to or greater than 6");
        City city = this.getCity(code);
        return city == null ? null : city.getCountyCollection().get(code.substring(0, 6));
    }

    public Town getTown(String code) {
        if (code == null || code.length() < 9)
            throw new ArgumentException("length of 'code' must equals to or greater than 9");
        County county = this.getCounty(code);
        return county == null ? null : county.getTownCollection().get(code.substring(0, 9));
    }
}
